package com.ruoyi.student.controller;

import com.ruoyi.college.domain.College;
import com.ruoyi.college.service.ICollegeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.StudentBaodao;
import com.ruoyi.student.domain.StudentInfo;
import com.ruoyi.student.domain.StudentRegistration;
import com.ruoyi.student.service.IStudentBaodaoService;
import com.ruoyi.student.service.IStudentInfoService;
import com.ruoyi.teacher.domain.teacher;
import com.ruoyi.teacher.service.IteacherService;
import com.ruoyi.trainingManagement.domain.CourseScheduleDetails;
import com.ruoyi.student.service.IStudentRegistrationService;
import com.ruoyi.trainingManagement.domain.trainingProgram;
import com.ruoyi.trainingManagement.domain.trainingProgramCourses;
import com.ruoyi.trainingManagement.service.ICourseScheduleDetailsService;
import com.ruoyi.trainingManagement.service.ItrainingProgramCoursesService;
import com.ruoyi.trainingManagement.service.ItrainingProgramService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;
@RestController
@RequestMapping("/student/alllist")
public class allListInfoController  extends BaseController {
    @Autowired
    private IStudentInfoService studentInfoService;
    @Autowired
    private ItrainingProgramService trainingProgramService;
    @Autowired
    private IStudentBaodaoService studentBaodaoService;
    @Autowired
    private IStudentRegistrationService studentRegistrationService;
    @Autowired
    private ItrainingProgramCoursesService trainingProgramCoursesService;
    @Autowired
    private ICourseScheduleDetailsService courseScheduleDetailsService;
    @Autowired
    private ICollegeService collegeService;
    @Autowired
    private IteacherService teacherService;
    @Autowired
    private SysDeptServiceImpl sysDeptService;
    @Autowired
    private SysRoleMapper roleMapper;
    /**
     * 根据学院id列表查询培养方案管理列表
     */
    @GetMapping("/trainingprogramlist")
    public TableDataInfo selectTrainingProgramListByCollegeIds() {
        // 获取学院ID列表
        List<String> collegeIds = studentInfoService.selectCollegeIdsByDeptIds();

        // 分页处理（建议放在判断前，确保两种分支都分页）
        startPage();

        List<trainingProgram> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeIds)) {
            // 无学院ID限制，查询全部
            list = trainingProgramService.selecttrainingProgramList(new trainingProgram());
        } else {
            // 根据学院ID列表查询
            list = trainingProgramService.selectTrainingProgramIdsByCollegeIds(collegeIds);
        }

        return getDataTable(list);
    }
    //获取培养方案课程列表
    @GetMapping("/trainingprogramcourseslist")
    public TableDataInfo selectTrainingProgramCoursesListByCollegeIds() {
        List<String> collegeIds = studentInfoService.selectCollegeIdsByDeptIds();

        // 分页处理（建议放在判断前，确保两种分支都分页）
        //startPage();

        List<trainingProgramCourses> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeIds)) {
            List<Integer> list1 = new ArrayList<>();
            list1.add(1);
            System.out.println("list1.add(1)"+list1);
            list =trainingProgramCoursesService.selectByStatusList(list1);
            System.out.println("待加入排课任务获取到的培养方案课程列表------list"+list);
        } else {
            // 根据学院ID列表查询
            list = trainingProgramCoursesService.selectCoursesByCollegeIds(collegeIds);
            System.out.println( "待加入排课任务获取到的培养方案课程列表------list"+list);
        }
        return getDataTable(list);
    }
    //根据学院ID列表联查培养方案课程列表
    @GetMapping("/trainingprogramcourseslistbycollegeids")
    public TableDataInfo selectTrainingProgramCoursesListByCollegeIdswhereCourse() {
        List<String> collegeIds = studentInfoService.selectCollegeCodesByDeptIds();
        System.out.println( "获取到的学院ID列表------collegeIds"+collegeIds);
        // 分页处理（建议放在判断前，确保两种分支都分页）
        //startPage();

        List<trainingProgramCourses> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeIds)) {
            List<Integer> list1 = new ArrayList<>();
            list1.add(2);
            System.out.println("list1.add(1)"+list1);
            list =trainingProgramCoursesService.selectByStatusList(list1);
            System.out.println("当前排课任务获取到的培养方案课程列表------list"+list);
        } else {
            // 根据学院ID列表查询
            list = trainingProgramCoursesService.selectTrainingProgramCoursesByCollegeIds(collegeIds);
            System.out.println( "当前排课任务获取到的培养方案课程列表------list"+list);
        }
        return getDataTable(list);
    }
    //获取课程排课详细列表
    @GetMapping("/coursedetailslist")
    public TableDataInfo selectCourseScheduleDetailsList() {
        List<String> collegeIds = studentInfoService.selectCollegeCodesByDeptIds();
        System.out.println( "获取到的学院ID列表------collegeIds"+collegeIds);
        // 分页处理（建议放在判断前，确保两种分支都分页）
        //startPage();

        List<CourseScheduleDetails> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeIds)) {
            list =courseScheduleDetailsService.selectCourseScheduleDetailsList(new CourseScheduleDetails());
        } else {
            // 根据学院ID列表查询
            list = courseScheduleDetailsService.selectCourseScheduleDetailsByCollegeIds(collegeIds);
            System.out.println( "课程排课详细列表---CourseScheduleDetails---list"+list);
        }
        return getDataTable(list);
    }
    @GetMapping("/studentlist")
    public TableDataInfo selectStudentListByCollegeIds(StudentInfo studentInfo) {
        // 获取学院ID列表
        List<String> collegeCodes = studentInfoService.selectCollegeCodesByDeptIds();
        // 分页处理（建议放在判断前，确保两种分支都分页）
        startPage();

        List<StudentInfo> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeCodes)) {
            // 无学院ID限制，查询全部
            list = studentInfoService.selectStudentInfoList(studentInfo);
        } else {
            // 根据学院ID列表查询
            list = studentInfoService.selectStudentIdsByCollegeCodes(collegeCodes,studentInfo);
        }
        return getDataTable(list);
    }
    @GetMapping("/studentbaodaolist")
    public TableDataInfo selectStudentbaodaoListByCollegeIds(StudentBaodao studentBaodao) {
        // 获取学院ID列表
        List<String> collegeCodes = studentInfoService.selectCollegeCodesByDeptIds();
        // 分页处理（建议放在判断前，确保两种分支都分页）
        startPage();

        List<StudentBaodao> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeCodes)) {
            // 无学院ID限制，查询全部
            list = studentBaodaoService.selectStudentBaodaoList(studentBaodao);
        } else {
            // 根据学院ID列表查询
            list = studentBaodaoService.selectStudentBaodaoIdsByCollegeCodes(collegeCodes,studentBaodao);
        }

        return getDataTable(list);
    }
    //获取学院信息列表
    @GetMapping("/collegelist")
    public TableDataInfo selectCollegeList() {
        // 获取学院ID列表
        List<String> collegeIds = studentInfoService.selectCollegeIdsByDeptIds();
        // 分页处理（建议放在判断前，确保两种分支都分页）
        //startPage();

        List<College> list = Collections.emptyList();
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeIds)) {
            // 无学院ID限制，查询全部
            list = collegeService.selectCollegeList(new College());
            System.out.println("无学院ID限制获取的学院信息列表---College---list"+list);
        } else {
            // 根据学院ID列表查询
            list=collegeService.selectCollegesByCollegeIds(collegeIds);
            System.out.println("获取的学院信息列表---College---list"+list);
        }

        return getDataTable(list);
    }
    //根据学院ID列表查询对应的教师信息列表
    @GetMapping("/teacherlistbycollegeids")
    public TableDataInfo selectTeacherListByCollegeIds() {
        // 获取学院ID列表
        List<String> collegeCodes = studentInfoService.selectCollegeCodesByDeptIds();
        System.out.println("selectTeacherListByCollegeIds获取到的学院ID列表------collegeCodes" + collegeCodes);
        // 分页处理（建议放在判断前，确保两种分支都分页）
        //startPage();

        List<teacher> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeCodes)) {
            // 无学院ID限制，查询全部
            list = teacherService.selectteacherList(new teacher());
            System.out.println("selectteacherList无学院ID限制获取的教师信息列表---teacher---list" + list);
        } else {
            // 根据学院ID列表查询
            list = teacherService.selectTeachersByCollegeIds(collegeCodes);
            System.out.println("selectTeachersByCollegeIds获取的教师信息列表---teacher---list" + list);
        }
        return getDataTable(list);
    }
    @GetMapping("/studentregistrationlist")
    public TableDataInfo selectStudentregistrationListByCollegeIds(StudentRegistration studentRegistration) {
        // 获取学院ID列表
        List<String> collegeCodes = studentInfoService.selectCollegeCodesByDeptIds();
        // 分页处理（建议放在判断前，确保两种分支都分页）
        startPage();

        List<StudentRegistration> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeCodes)) {
            // 无学院ID限制，查询全部
            list = studentRegistrationService.selectStudentRegistrationList(studentRegistration);
        } else {
            // 根据学院ID列表查询
            list = studentRegistrationService.selectStudentRegistrationIdsByCollegeCodes(collegeCodes,studentRegistration);
        }

        return getDataTable(list);
    }
    @GetMapping("/CourseScheduleDetailsList")
    public TableDataInfo list(CourseScheduleDetails courseScheduleDetails) {
        // 获取登录用户的角色对应的部门ID列表
        List<Long> roleDeptIds = sysDeptService.getuserRoleDeptIds();
        // 获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 获取用户角色列表
        List<SysRole> userRoles = roleMapper.selectRolePermissionByUserId(loginUser.getUserId());
        // 标记是否为admin或GraduateOffice角色
        boolean isAdminOrGraduateOffice = userRoles.stream()
                .anyMatch(role -> "admin".equals(role.getRoleKey()) || "GraduateOffice".equals(role.getRoleKey()));
        // 标记是否为teacher角色
        boolean isTeacher = userRoles.stream()
                .anyMatch(role -> "teacher".equals(role.getRoleKey()));
        List<CourseScheduleDetails> list = Collections.emptyList();
        if (roleDeptIds.isEmpty()) {
            // 情况1：部门ID列表为空时，根据用户身份进一步判断
            if (isAdminOrGraduateOffice) {
                // 1.1 admin或GraduateOffice角色：全量加载数据
                list = courseScheduleDetailsService.selectCourseScheduleDetailsList(courseScheduleDetails);
            } else if (isTeacher) {
                // 1.2 teacher角色：使用教师专属方法加载数据
                //list = courseScheduleDetailsService.selectTeacherCourseScheduleDetailsList(courseScheduleDetails, loginUser.getUserId());
            } else {
                // 1.3 其他角色：默认返回空列表（或根据业务需求调整）
                list = new ArrayList<>();
            }
        } else {
            // 情况2：部门ID列表有值时，使用部门筛选方法加载数据
            //list = courseScheduleDetailsService.selectCourseScheduleDetailsByDeptIds(courseScheduleDetails, roleDeptIds);
        }
        return getDataTable(list);
    }
}
